from lxml import etree
from urllib import request
from db.dao import MongoDao
import re
import time
import pymongo
import uuid

"""
http://m.qqwangming.com.cn/nvshengwangming/sigezi/list32.html
"""


def get_nick_name_list(page_no, uri_type):
    response = request.urlopen("http://m.qqwangming.com.cn/nvshengwangming/%s/list%d.html" % (uri_type, page_no))
    page = response.read().decode("utf-8")
    html_page = etree.HTML(page)

    nick_name_list = html_page.xpath("//ul[@class='article-list']/li//p[2]/text()")
    final_nick_name_list = []
    for nick_name in  nick_name_list:
        final_nick_name_list.append(filter_str(nick_name))
    return final_nick_name_list


def filter_str(string):
    string = re.sub("[^\u4e00-\u9fa5]", "", string)
    string = string.replace(" ", "")
    string = string.strip()
    return string


def get_timestamp():
    return int(round(time.time() * 1000))


def get_uuid():
    return str(uuid.uuid1()).replace("-", "")


def save_nick_name(s_mongodb_dao, s_nick_name, s_type):
    nick_name_dict = dict()
    nick_name_dict['id'] = get_uuid()
    nick_name_dict['name'] = s_nick_name
    nick_name_dict['type'] = s_type
    nick_name_dict['status'] = 0
    nick_name_dict['createTime'] = get_timestamp()
    s_mongodb_dao.insert(nick_name_dict)


def generate_nick_name(uri_type, s_type):

    mongodb_dao = MongoDao("nickname")
    for page_no in range(1, 100):
        nick_name_list_x = get_nick_name_list(page_no, uri_type)
        if len(nick_name_list_x) == 0:
            print("Fetch %s page data." % (page_no-1))
            break
        for nick_name in nick_name_list_x:
            if len(nick_name) < 4:
                print("Skip Name --->[%s] too short." % nick_name)
                continue
            try:
                save_nick_name(mongodb_dao, nick_name, s_type)
            except pymongo.errors.DuplicateKeyError:
                print("重复昵称-->:[%s]" % nick_name)
        time.sleep(5)


if __name__ == '__main__':
    # 四个字->1， 五个字->2， 六个字->3， 七个字->4，唯美->5
    """
    
    nick_name_type = [
        {"name": "sigezi", "type": 1}, {"name": "wugezi", "type": 2}, {"name": "liugezi", "type": 3}, {"name": "qigezi", "type": 4}, {"name": "weimei", "type": 5},
        {"name": "keai", "type": 6}, {"name": "xiaoqingxin", "type": 7}, {"name": "wenyi", "type": 8}, {"name": "jiandan", "type": 9}, {"name": "neihan", "type": 10},
        {"name": "shanggan", "type": 11}, {"name": "aiqing", "type": 12}, {"name": "baqi", "type": 13}, {"name": "chaozhuai", "type": 14}, {"name": "gexing", "type": 15},
        {"name": "feizhuliu", "type": 16}, {"name": "beishang", "type": 17}, {"name": "koukou", "type": 18}, {"name": "youxi", "type": 19}, {"name": "chengshu", "type": 20},
        {"name": "lizhi", "type": 21}, {"name": "youya", "type": 22}, {"name": "koukou", "type": 23}, {"name": "youxi", "type": 24}, {"name": "chengshu", "type": 25}
    ]
    """
    nick_name_type = [
        {"name": "koukou", "type": 23}, {"name": "youxi", "type": 24}, {"name": "chengshu", "type": 25}
    ]
    for ele in nick_name_type:
        name = ele['name']
        d_type = ele['type']
        print("name-->[%s] type--->[%s]" % (name, d_type))
        generate_nick_name(name, d_type)
